<?php

	include("config.php");
	//获取登录页
	$snoopy = new Snoopy;
	//模拟HEADER
	$snoopy->agent = $agent;
	$snoopy->host = IP;
	//获取分析表单
	$snoopy->fetchform($loginurl);
	$res = $snoopy->results;

	//超时返回
	if ($snoopy->status != '200' || $snoopy->timed_out) {
    	echo json_err(1,"登录页获取超时！");
    	return;
	}

	//首次设置COOKIE
	$snoopy->setcookies();

	$_SESSION["__VIEWSTATE"]          = substr($res[1], 64,132);
	$_SESSION["__VIEWSTATEGENERATOR"] = substr($res[2], 82,8);
	$_SESSION["ASP_NET_SessionId"]    = $snoopy->cookies['ASP.NET_SessionId'];
	//var_dump($_SESSION);
	//获取验证码
	$snoopy2 = new Snoopy;

	//模拟HEADER
	$snoopy->agent = $agent;
	$snoopy2->host = IP;
	$snoopy2->referer = $loginurl;
	$snoopy2->cookies['ASP.NET_SessionId'] = $_SESSION["ASP_NET_SessionId"];

	//获取存储验证图片
	$snoopy2->fetch($picurl);
	$res2 = $snoopy2->results;

	//超时返回
	if ($snoopy2->status != '200' || $snoopy2->timed_out) {
    	echo json_err(1,"验证码获取超时！");
    	return;
	}

	$pic_data = "data:image/gif;base64,".base64_encode($res2);

	$session_id = session_id();
  	if(!empty($_GET['noapi'])){
?>
		<!DOCTYPE html>
		<html>
		<head>
			<title>KillNR</title>
			<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		</head>
		<body>

		<form action="login.php" method="POST" name="form" target="_blank">
			学号：<input type="text" name="UserID" value="S15085208022">
			密码：<input type="password" name="PassWord" value="172223">
			查询类型：
			<select id="get_type" onchange="document.form.action=this.value;">
				<option value="login.php">验证密码</option>
				<option value="login_info.php">个人信息</option>
				<option value="login_table.php">个人课表</option>
				<option value="login_exam.php">个人考试</option>
				<option value="login_score.php">个人成绩</option>
			</select>
			验证码：<input type="text" name="cCode">
			<img src="<?php echo $pic_data; ?>" />
			<input type="hidden" name="noapi" value="1">
			<input type="hidden" name="session_id" value="<?php echo $session_id;?>">
			<input type="submit" name="submit" value="提交">
		</form>
		</body>
		</html>
<?php
	}else{
		echo json_encode(array('pic_data'=>$pic_data,'session_id'=>$session_id));
		//echo '<img src="'.$pic_data.'" />'.$session_id;
	}
	//mysql_close($link);
?>
